/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"42BC0600-90F8-4202-B890-7D396EF41936",variableType:4}
 */
var gUservID = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"edeea83f-567e-4d69-a9fe-892b34cd0b07"}
 */
var gLoginPass = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"56a340ee-6d56-4694-9c11-62e672325257"}
 */
var gLoginUserv = null;

/**
 *
 * @properties={typeid:24,uuid:"cc62cd0e-79f7-41a2-b955-d75c11a80c29"}
 */
function iraGruposDtl()
{
	forms.grupostreeDtl.controller.show()
}

/**
 *
 * @properties={typeid:24,uuid:"0605d2f3-b799-4b98-956e-e187e0e9abb2"}
 */
function iraGruposTreeLst()
{
	forms.grupostreeLst.controller.show()
}

/**
 *
 * @properties={typeid:24,uuid:"e5ea5c16-857a-43d4-889a-c662c48224d6"}
 */
function iraPantallasDtl()
{
	forms.pantallasDtl.controller.show()
}

/**
 *
 * @properties={typeid:24,uuid:"b91b1ecc-d0f7-429b-8f9d-ef6ce5ec473a"}
 */
function iraPantallasLst()
{
	forms.pantallasLst.controller.show()
}

/**
 *
 * @properties={typeid:24,uuid:"dbf9dd74-efa1-46dd-8fe2-a5f78f4c1fec"}
 */
function iraSolucionesDtl()
{
	forms.solucionesDtl.controller.show()
}

/**
 *
 * @properties={typeid:24,uuid:"14050157-cf18-4d9b-bc72-5707c272e44b"}
 */
function iraSolucionesLst()
{
	forms.solucionesLst.controller.show()
}

/**
 *
 * @properties={typeid:24,uuid:"c85b7ece-c82b-44f9-9020-09be15a30bcb"}
 */
function iraUservsDtl()
{
	forms.uservsDtl.controller.show()
}

/**
 *
 * @properties={typeid:24,uuid:"1A84CB74-02B2-4E7D-B4F1-0103BEA5FF2E"}
 */
function hashPass(p) {
	return utils.stringMD5HashBase64(p)	
}

/**
 * @properties={typeid:24,uuid:"E63B3693-C3A3-4B1E-BCC8-93465E19AB43"}
 */
function salir() {
	security.logout()
	application.exit()
}

/**
 * Called when the valuelist needs data, it has 3 modes.
 * real and display params both null: return the whole list
 * only display is specified, called by a typeahead, return a filtered list
 * only real value is specified, called when the list doesnt contain the real value for the give record value, this will insert this value into the existing list
 *
 * @param {String} displayValue The value of a lookupfield that a user types
 * @param realValue The real value for a lookupfield where a display value should be get for
 * @param {JSRecord<db:/fwk_navigation_infocom/pantallas>} record The current record for the valuelist.
 * @param {String} valueListName The valuelist name that triggers the method. (This is the FindRecord in find mode, which is like JSRecord has all the columns/dataproviders, but doesn't have its methods)
 * @param {Boolean} findMode True if foundset of this record is in find mode
 *
 * @returns {JSDataSet} A dataset with 1 or 2 columns display[,real]
 *
 * @properties={typeid:24,uuid:"BF1ACD0E-C535-45C9-B005-C1745B05044B"}
 */
function getDataSetForValueListGrupos(displayValue, realValue, record, valueListName, findMode) {
	if (displayValue == null && realValue == null) {
		var args = [record.id_solucion];
		var $ds = databaseManager.getDataSetByQuery("fwk_navigation_infocom", "select descripcion, nrogrupo from grupostree where solucion = ?", args, -1);
		$ds.addRow(1,['Ninguno',0])
		return $ds
	} else if (displayValue != null) {
		var args = [displayValue + "%", record.id_solucion]
		var $ds = databaseManager.getDataSetByQuery("fwk_navigation_infocom", "select descripcion, nrogrupo from grupostree where descripcion like ? and solucion = ?", args, -1);
		$ds.addRow(1,['Ninguno',0])
		return $ds
	} else if (realValue != null) {
		var args = [realValue, record.id_solucion];
		var $ds = databaseManager.getDataSetByQuery("fwk_navigation_infocom", "select descripcion, nrogrupo from grupostree where nrogrupo = ? AND solucion = ?", args, 1);
		$ds.addRow(1,['Ninguno',0])
		return $ds
	}
	return null;

}

/**
 * 
 * @param {String} nombreForm
 * @properties={typeid:24,uuid:"5F0C7EA4-AAF9-43AE-BDC5-2EB1EB41DD4C"}
 */
function pasaraCsv(nombreForm) {
	
	if (application.getApplicationType() == APPLICATION_TYPES.WEB_CLIENT)
	{
		globals.DIALOGS.showWarningDialog('Error','Esta opción no está disponible para cliente web','OK','CerrarForm');
	}
	else
	{
		var tbl = databaseManager.getTable( forms[nombreForm].controller.getDataSource())
		var cols = tbl.getColumnNames()
		var pk = tbl.getRowIdentifierColumnNames()[0]
		
		var jsFile = plugins.file.showFileSaveDialog( 'c:/'+tbl.getSQLName()+'.csv')
		if (jsFile)
		{
			var fw = new Packages.java.io.FileWriter(jsFile.getAbsolutePath());
			var bw = new Packages.java.io.BufferedWriter(fw);
			
			//ESCRIBO EL NOMBRE DE LAS COLUMNAS EN LA PRIMER LINEA DEL ARCHIVO
			var linea = ''
			for (var j = 0 ; j < cols.length ; j++)
			{
				if (cols[j] != pk)
				{
					if (j != 1)
						linea += ';'
					linea += cols[j]
				}
			}
			bw.write(linea,0,linea.length)
			linea = ''
			var fs = forms[nombreForm].foundset
			
			var i = 1
			var $total = databaseManager.getFoundSetCount(fs)
			while (i <= $total)
			{
				fs.setSelectedIndex(i)
				for (j = 0 ; j < cols.length ; j++)
				{
					if (cols[j] != pk)
					{
						var campo = fs.getDataProviderValue(cols[j])
						if (j != 1)
							linea += ';'
						linea += campo
					}
				}
				bw.newLine()
				bw.write(linea,0,linea.length)
				linea = ''
				i++
			}
			
			bw.close()
			fw.close()
			globals.DIALOGS.showInfoDialog('Exito',jsFile.getAbsolutePath().replace("\\","/") + ' creado con exito','OK');
		}
	}
}

/**
 * @param {String} dataSource
 * 
 * @properties={typeid:24,uuid:"B86316BF-5F16-427C-89C0-8D4CAD3C771F"}
 */
function cargarCsv(dataSource) {
	/** @type {plugins.file.JSFile}*/
	var arch = plugins.file.showFileOpenDialog(1, null,  false,  new Array ("TXT,CSV","txt","csv"));
	if (arch != null)
	{
		var fr = new Packages.java.io.FileReader(arch.getAbsolutePath());
		var br = new Packages.java.io.BufferedReader(fr);
		/** @type {String} */ 
		var linea = br.readLine() // Leo la primer linea
		
		if (linea == null)
		{
			return 2
		}
		else
		{
			var $columnas = linea.split(';')
			linea = br.readLine()
			var $fs = databaseManager.getFoundSet(dataSource)
						
			while (linea != null) //Hasta que termine el archivo
			{
				var $datos = linea.split(';')
				$fs.newRecord()
				for (var i = 0 ; i < $datos.length ; i++)
				{
					if ($datos[i])
						$fs[$columnas[i]] = $datos[i]
				}
				
				linea = br.readLine() //Leo una linea del archivo
			}
		}
		br.close();
		fr.close();
	}
	return 1
}

/**
 * Called when the valuelist needs data, it has 3 modes.
 * real and display params both null: return the whole list
 * only display is specified, called by a typeahead, return a filtered list
 * only real value is specified, called when the list doesnt contain the real value for the give record value, this will insert this value into the existing list
 *
 * @param {String} displayValue The value of a lookupfield that a user types
 * @param realValue The real value for a lookupfield where a display value should be get for
 * @param {JSRecord} record The current record for the valuelist.
 * @param {String} valueListName The valuelist name that triggers the method. (This is the FindRecord in find mode, which is like JSRecord has all the columns/dataproviders, but doesn't have its methods)
 * @param {Boolean} findMode True if foundset of this record is in find mode
 * @param {Boolean} rawDisplayValue The raw displayValue without being converted to lower case
 *
 * @returns {JSDataSet} A dataset with 1 or 2 columns display[,real]
 *
 * @properties={typeid:24,uuid:"04044F8B-7380-4DC2-8E7E-7566C6856329"}
 */
function getDataSetForValueListVL(displayValue, realValue, record, valueListName, findMode, rawDisplayValue) {
	var valueLists = application.getValueListNames()//solutionModel.getValueLists();
	valueLists[valueLists.indexOf('valuelists',0)] = null
	var ds = databaseManager.convertToDataSet(valueLists)
	
	if (displayValue == null && realValue == null) {
		return ds
	}
	return null
}

/**
 * Callback method for when solution is opened.
 * When deeplinking into solutions, the argument part of the deeplink url will be passed in as the first argument
 * All query parameters + the argument of the deeplink url will be passed in as the second argument
 * For more information on deeplinking, see the chapters on the different Clients in the Deployment Guide.
 *
 * @param {String} arg startup argument part of the deeplink url with which the Client was started
 * @param {Object<Array<String>>} queryParams all query parameters of the deeplink url with which the Client was started
 *
 * @properties={typeid:24,uuid:"8EB998C6-6DD3-415B-8353-C8F13E404DAF"}
 */
function onSolutionOpen(arg, queryParams) {
	globals.gUservID = security.getUserUID()
	databaseManager.addTableFilterParam('fwk_navigation_infocom',null,'userv_id','=',globals.gUservID)
	
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"A632A52D-53C4-4F82-96BA-2386EB3C3B8D"}
 */
function iraParamApp(event) {
	forms.param_app_tbl.controller.show()
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"C4AA04FF-9E2C-4785-A8F1-61665B32C678"}
 */
function iraParamUsr(event) {
	forms.param_usr_tbl.controller.show()
}

/**
 * @properties={typeid:24,uuid:"520302FC-7E55-407F-8439-1D96EF3B0886"}
 */
function iraLog() {
	forms.log_tbl.controller.show()
}
